package com.irdstudio.cdp.pboc.service.impl;

import java.util.*;

import com.irdstudio.cdp.pboc.service.dao.PbocJobDao;
import com.irdstudio.cdp.pboc.service.domain.PbocJob;
import com.irdstudio.cdp.pboc.service.domain.ZxhtmlInfoOcc;
import com.irdstudio.cdp.pboc.service.vo.PbocJobVO;
import com.irdstudio.cdp.pboc.service.vo.ZxhtmlInfoOccVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.cdp.pboc.service.dao.ZxHtmlInfoDao;
import com.irdstudio.cdp.pboc.service.domain.ZXHtmlInfo;
import com.irdstudio.cdp.pboc.service.facade.ZXHtmlInfoService;
import com.irdstudio.cdp.pboc.service.vo.ZXHtmlInfoVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;

/**
 * @description:征信html服务
 * @date:2018年12月29日 上午10:59:10
 * @author:hengyh
 */
@Service("zxHtmlInfoService")
public class ZXHtmlInfoServiceImpl implements ZXHtmlInfoService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(ZXHtmlInfoServiceImpl.class);
	
	@Autowired
	private ZxHtmlInfoDao zxHtmlInfoDao;

	@Autowired
	private PbocJobDao pbocJobDao;

	@Override
	public int insert(ZXHtmlInfoVO vo) {
		if (logger.isDebugEnabled()) 
			logger.debug("当前新增数据为:"+ vo.toString());
		ZXHtmlInfo zxHtmlInfo = new ZXHtmlInfo();
		int num = 0;
		try {
			beanCopy(vo, zxHtmlInfo);
			num = zxHtmlInfoDao.insert(zxHtmlInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 
	 * 查询征信报告<br>
	 * 0.2:wanglihua:2019年3月21日 下午4:11:36:TODO <br>
	 * @author wanglihua
	 * @since 2019年3月21日 下午4:11:36
	 * @date 2019年3月21日 下午4:11:36
	 * @version 0.1 
	 */
	@Override
	public ZXHtmlInfoVO queryByPk(ZXHtmlInfoVO vo) {
		logger.debug("当前查询参数信息为:"+ vo);
		ZXHtmlInfoVO outZXHtmlInfoVo = new ZXHtmlInfoVO();
		try {
			ZXHtmlInfo queryZXHtmlInfo = new ZXHtmlInfo();
			beanCopy(vo, queryZXHtmlInfo);
			ZXHtmlInfo queryZXHtmlInfos = Optional.ofNullable(zxHtmlInfoDao.queryByPk(queryZXHtmlInfo)).orElseGet(ZXHtmlInfo::new);
			outZXHtmlInfoVo = beanCopy(queryZXHtmlInfos, new ZXHtmlInfoVO());
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
			throw e;
		}
		return outZXHtmlInfoVo;
	}

	@Override
	public List<ZXHtmlInfoVO> queryAllZXHtmlReports(List<String> reportIds) throws Exception {
		logger.debug("当前查询参数信息为:"+ reportIds.toString());
		List<ZXHtmlInfoVO> list = null;
		ZXHtmlInfo zxHtmlInfo  = new ZXHtmlInfo();
		String ris = "('";
		for (String str :reportIds){
			ris = ris + str + "','";
		}
		ris = ris.substring(0,ris.length()-2) + ")";
		zxHtmlInfo.setReportId(ris);
		try {
			list = (List<ZXHtmlInfoVO>) beansCopy(zxHtmlInfoDao.queryAllZXHtmlReportsByPage(zxHtmlInfo),ZXHtmlInfoVO.class);
			pageSet(list, zxHtmlInfo);
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
			throw e;
		}
		return list;
	}

	@Override
	public List<ZxhtmlInfoOccVO> queryListBycerCode(ZxhtmlInfoOccVO zxhtmlInfoOccVO) {
		PbocJobVO pbocJobVO = new PbocJobVO();
		List<ZxhtmlInfoOccVO> zxhtmlInfoOccVOList = new ArrayList<>();
		logger.info("当前查询参数信息为:" + zxhtmlInfoOccVO.toString());
		try {
			List<ZxhtmlInfoOcc> zxhtmlInfoOccs = zxHtmlInfoDao.queryListBycerCode(zxhtmlInfoOccVO);
			if (Objects.nonNull(zxhtmlInfoOccs) && !zxhtmlInfoOccs.isEmpty()) {
				for (ZxhtmlInfoOcc zxhtmlInfoOcc : zxhtmlInfoOccs) {
					pbocJobVO.setFId(zxhtmlInfoOcc.getReportId());
					List<PbocJob> pbocJobs = pbocJobDao.queryByReportId(pbocJobVO);
					List<PbocJobVO> list =  (List<PbocJobVO>)beansCopy(pbocJobs, PbocJobVO.class);
					zxhtmlInfoOcc.setPbocJobVOS(list);
					zxhtmlInfoOccVOList = (List<ZxhtmlInfoOccVO>) beansCopy(zxhtmlInfoOccs, ZxhtmlInfoOccVO.class);
				}
				return zxhtmlInfoOccVOList;
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public List<ZXHtmlInfoVO> queryZXHtmlReportsByCus(ZXHtmlInfoVO param) throws Exception{
		logger.debug("当前查询参数信息为:"+ param.toString());
		List<ZXHtmlInfoVO> list = null;
		ZXHtmlInfo zxHtmlInfo  = new ZXHtmlInfo();
		try{
			beanCopy(param,zxHtmlInfo);
			list = (List<ZXHtmlInfoVO>) beansCopy(zxHtmlInfoDao.queryCusZXHtmlReportsByPage(zxHtmlInfo),ZXHtmlInfoVO.class);
			pageSet(list, zxHtmlInfo);
		}catch (Exception e) {
			logger.error("查询数据发生异常!", e);
			throw e;
		}

		return list;
	}
}
